<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./css/swiper.css">
    <script src="./js/vue.js"></script>
    <script src="./js/swiper.js"></script>
</head>
<body>
    <div id="app">
        <swiper-box>
            <div class="swiper-slide" style="height: 100px; background-color: brown;">slider1</div>
            <div class="swiper-slide" style="height: 100px; background-color: cadetblue;">slider2</div>
            <div class="swiper-slide" style="height: 100px; background-color: chocolate;">slider3</div>
        </swiper-box>
        <hr>
        <swiper-box :options="opt1">
            <div class="swiper-slide" style="height: 100px; background-color: brown;">slider1</div>
            <div class="swiper-slide" style="height: 100px; background-color: cadetblue;">slider2</div>
            <div class="swiper-slide" style="height: 100px; background-color: chocolate;">slider3</div>
            <template v-slot:view>
                <div class="swiper-pagination"></div>
            </template>
        </swiper-box>

    </div>

    <template id="SwiperBox">
        <div class="swiper-container" ref="loop">
            <div class="swiper-wrapper">
                <slot></slot>
            </div>
            <slot name="view"></slot>
        </div>
    </template>
</body>
<script>
    Vue.component("SwiperBox",{
        template:"#SwiperBox",
        props:{
            options:{
                type:Object,
                default(){
                    return {};
                }
            }
        },
        methods: {
            initSwiper(){
                //2等到页面渲染完成后再执行
                this.$nextTick(() => {
                    new Swiper(this.$refs.loop,this.options);
                });
            }
        },
        // 1等组件实例与模板完成关联后调用初始化swiper
        mounted() {
            this.initSwiper();
        },
    });

    new Vue({
        el:"#app",
        data:{
            opt1:{
                autoplay: true,
                pagination: {
                    el: '.swiper-pagination',
                }
            }
        }
    });
</script>

</html>